// pages/favorites/favorites.js
Page({
  data: {
    favorites: [],
    isEmpty: true
  },

  onLoad() {
    this.loadFavorites()
  },

  onShow() {
    this.loadFavorites()
  },

  // 加载收藏的消息
  loadFavorites() {
    const favorites = wx.getStorageSync('favoriteMessages') || []
    this.setData({
      favorites,
      isEmpty: favorites.length === 0
    })
  },

  // 删除收藏
  deleteFavorite(e) {
    const favoriteId = e.currentTarget.dataset.favoriteId
    
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这条收藏吗？',
      success: (res) => {
        if (res.confirm) {
          let favorites = wx.getStorageSync('favoriteMessages') || []
          favorites = favorites.filter(fav => fav.id !== favoriteId)
          
          wx.setStorageSync('favoriteMessages', favorites)
          
          this.setData({
            favorites,
            isEmpty: favorites.length === 0
          })
          
          wx.showToast({
            title: '已删除收藏',
            icon: 'success'
          })
        }
      }
    })
  },

  // 复制收藏内容
  copyFavorite(e) {
    const content = e.currentTarget.dataset.content
    wx.setClipboardData({
      data: content,
      success: () => {
        wx.showToast({
          title: '已复制到剪贴板',
          icon: 'success'
        })
      }
    })
  },

  // 清空所有收藏
  clearAllFavorites() {
    if (this.data.favorites.length === 0) {
      return
    }
    
    wx.showModal({
      title: '确认清空',
      content: '确定要清空所有收藏吗？此操作不可恢复。',
      success: (res) => {
        if (res.confirm) {
          wx.removeStorageSync('favoriteMessages')
          
          this.setData({
            favorites: [],
            isEmpty: true
          })
          
          wx.showToast({
            title: '已清空收藏',
            icon: 'success'
          })
        }
      }
    })
  },

  // 分享收藏内容
  shareFavorite(e) {
    const content = e.currentTarget.dataset.content
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },

  // 返回聊天页面
  goToChat() {
    wx.switchTab({
      url: '/pages/chat/chat'
    })
  }
})